package com.decorate.demo03;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FilterReader;
import java.io.IOException;

public class GrepReader extends FilterReader {
    private BufferedReader in;
    private String subStr;
    private int lineNum;

    /**
     * Creates a new filtered reader.
     *
     * @param in a Reader object providing the underlying stream.
     * @throws NullPointerException if <code>in</code> is <code>null</code>
     */
    public GrepReader(FileReader in, String subString) {
        super(in);
        this.in = new BufferedReader(in);
        this.subStr = subString;
        lineNum = 0;
    }

    /**
     * 从流中读取一行,并检索该行是否包含待检索的字符串
     */
    public final String readLine() throws IOException {
        String line;
        do {
            line = in.readLine();
            lineNum++;
        }while ((line != null) && (line.indexOf(subStr) ==-1));
        return line;
    }

    public int lineNum(){
        return lineNum;
    }


}
